package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao {

    static Student[] stus = new Student[5];

    //在库管中将数组返回给上级
    public Student[] findAllStudent() {
        return stus;
    }

    //在Dao中进行添加方法的逻辑编写,并将返回值最终返回给controller
    public boolean addStudent(Student stu) {
        //先给定索引为-1，假设数组中都填满了
        //遍历数组，取出每一个元素，判断是否为null，如果为null，让index索引记录当前索引
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        //如果index为-1，表示填满了，返回false，否则没有填满，将封装的学生赋值给数组index返回true
        if (index == -1) {
            return false;
        } else {
            stus[index] = stu;
            return true;
        }
    }


    //删除ID方法
    public void deleteStudentById(String delId) {
        //查找id在数组的所在位置
        int index = getIndex(delId);
        //将该索引位置用null覆盖
        stus[index] = null;
    }


    ////查找id在数组的所在位置方法
    public int getIndex(String delId) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = StudentDao.stus[i];
            if (stu != null && stu.getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改ID的方法
    public void updateStudentById(String updateId, Student newStu) {
        int index = getIndex(updateId);
        stus[index] = newStu;


    }
}
